package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.api.AnalysisWebsiteParamRpcService;
import com.hunttown.mes.rpc.domain.AnalysisWebsiteParamDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisWebsiteParamDTOQuery;
import com.hunttown.mes.service.AnalysisWebsiteParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.AnalysisWebsiteParamConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 15:07:24
 */
@Service
public class AnalysisWebsiteParamRpcServiceImpl implements AnalysisWebsiteParamRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisWebsiteParamRpcServiceImpl.class);

    private final AnalysisWebsiteParamService analysisWebsiteParamService;

    @Autowired
    public AnalysisWebsiteParamRpcServiceImpl(AnalysisWebsiteParamService analysisWebsiteParamService) {
        this.analysisWebsiteParamService = analysisWebsiteParamService;
    }

    @Override
    public AnalysisWebsiteParamDTO insert(AnalysisWebsiteParamDTO analysisWebsiteParamDTO) {
        try {
            return convert(analysisWebsiteParamService.insert(convert(analysisWebsiteParamDTO)));
        } catch (Exception e) {
            logger.error("AnalysisWebsiteParam create failed.", e);
            throw new RuntimeException("AnalysisWebsiteParam create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisWebsiteParamDTO analysisWebsiteParamDTO) {
        try {
            return analysisWebsiteParamService.updateInfoById(convert(analysisWebsiteParamDTO));
        } catch (Exception e) {
            logger.error("AnalysisWebsiteParam update by id failed.", e);
            throw new RuntimeException("AnalysisWebsiteParam update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisWebsiteParamDTOQuery analysisWebsiteParamDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisWebsiteParamDTOQuery.getW_id() != null && analysisWebsiteParamDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisWebsiteParamDTOQuery.getW_id());
            }

            if (analysisWebsiteParamDTOQuery.getIds() != null && !analysisWebsiteParamDTOQuery.getIds().equals("")) {
                query.put("ids", analysisWebsiteParamDTOQuery.getIds());
            }

            return analysisWebsiteParamService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisWebsiteParam update by query failed.", e);
            throw new RuntimeException("AnalysisWebsiteParam update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisWebsiteParamDTO selectById(Integer id) {
        try {
            return convert(analysisWebsiteParamService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisWebsiteParam select by id failed.", e);
            throw new RuntimeException("AnalysisWebsiteParam select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisWebsiteParamDTO selectByDomain(String domain) {
        try {
            return convert(analysisWebsiteParamService.selectByDomain(domain));
        } catch (Exception e) {
            logger.error("AnalysisWebsiteParam select by domain failed.", e);
            throw new RuntimeException("AnalysisWebsiteParam select by domain failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisWebsiteParamDTO selectObjByQuery(AnalysisWebsiteParamDTOQuery analysisWebsiteParamDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisWebsiteParamDTOQuery.getEntId() != null) {
                query.put("entId", analysisWebsiteParamDTOQuery.getEntId());
            }

            return convert(analysisWebsiteParamService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisWebsiteParam select obj by query failed.", e);
            throw new RuntimeException("AnalysisWebsiteParam select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisWebsiteParamDTO> getListForPage(AnalysisWebsiteParamDTOQuery analysisWebsiteParamDTOQuery) {
        Query query = Query.page(analysisWebsiteParamDTOQuery.getPn(), analysisWebsiteParamDTOQuery.getPageSize());

        if (analysisWebsiteParamDTOQuery.getIds() != null && !analysisWebsiteParamDTOQuery.getIds().equals("")) {
            query.put("ids", analysisWebsiteParamDTOQuery.getIds());
        }

        if (analysisWebsiteParamDTOQuery.getLikeName() != null && !analysisWebsiteParamDTOQuery.getLikeName().equals("")) {
            query.put("likeName", analysisWebsiteParamDTOQuery.getLikeName());
        }

        if (analysisWebsiteParamDTOQuery.getOrderBy() != null && analysisWebsiteParamDTOQuery.getOrderBy().length() > 0) {
            query.put("orderBy", analysisWebsiteParamDTOQuery.getOrderBy());
        }

        List<AnalysisWebsiteParamDTO> list = convert(analysisWebsiteParamService.selectByQuery(query));
        Page<AnalysisWebsiteParamDTO> page = Page.from(analysisWebsiteParamDTOQuery.getPn(), analysisWebsiteParamDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisWebsiteParamService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisWebsiteParamService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisWebsiteParam delete failed.", e);
            throw new RuntimeException("AnalysisWebsiteParam delete failed, Please order your code.", e);
        }
    }
}